package cn.com.cookie;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "delCookie", value = "/delCookie")
public class delCookie extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 要删除Cookie，需要创建一个同名的Cookie
        Cookie userCookie = new Cookie("username", "");
        // 设置路径，必须与创建时的路径一致
        userCookie.setPath("/");

        // 设置有效期为0，表示立即删除
        userCookie.setMaxAge(0);

        // 添加到响应中，发送给浏览器
        response.addCookie(userCookie);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
